/**
 *  @author 谭
 *  书处理函数
 */
"use strict";

const db = require('../../db/index');

const returnData = require('../../utils/returnData');

/*
    新增书类分类标签
 */
exports.addBooksClassIfy = (req,res) => {
    try {
        let time = new Date();
        const sql = 'insert into classify set name=?,time=?';
        db.query(sql,[req.body.name,time],(err,result) => {
            if (err) return  returnData(res,500,err);
            if (result.affectedRows !== 1) return returnData(res,400,'新增失败');
            return returnData(res,200,'新增成功');
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    获取书的分类标签
 */
exports.getBooksClassIfy = (req,res) => {
    try {
        const sql = 'select * from classify where isDel=0';
        db.query(sql,(err,result) => {
            if (err) return  returnData(res,500,err);
            return returnData(res,200,'获取成功',{data: result});
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    删除书的分类
 */
exports.delBooksClassify = (req,res) => {
    try {
        const sql = 'update classify set isDel=1 where id=?';
        db.query(sql,req.params.id,(err,result) => {
            if (err) return  returnData(res,500,err);
            if (result.affectedRows !== 1) return returnData(res,400,'删除失败');
            return returnData(res,200,'删除成功');
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}



/*
    录入书
 */
exports.addBooks = (req,res) => {
    try {
        let time = new Date();
        const sql = 'insert into books set bookName=?,author=?,time=?,img=?,publicAccounts=?,classIfy=?,number=?';
        db.query(sql,[req.body.bookName,req.body.author,time,req.body.img,req.user.accounts,req.body.classIfy,req.body.number],(err,result) => {
           if (err) return  returnData(res,500,err);
           if (result.affectedRows !== 1) return returnData(res,400,'新增失败');
           return returnData(res,200,'新增成功');
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}


/*
    获取书籍
 */
exports.getBooks = (req,res) => {
    try {
        let m = (parseInt(req.body.pageNumber) - 1) * parseInt(req.body.pageSize);
        let n =  parseInt(req.body.pageNumber) * parseInt(req.body.pageSize);
        const sql = 'select * from books where isDel=0 order by time desc limit ?,?';
        db.query(sql,[m,n],(err,result) => {
            if (err) return  returnData(res,500,err);
            return returnData(res,200,'获取成功',{data: result,total: req.body.total});
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    删除书籍
 */
exports.delBooks = (req,res) => {
    try {
        const sql = 'update books set isDel=1 where id=?';
        db.query(sql,req.params.id,(err,result) => {
            if (err) return  returnData(res,500,err);
            if (result.affectedRows !== 1) return returnData(res,400,'删除失败');
            return returnData(res,200,'删除成功');
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    用户借阅书
 */
exports.recordBooks = (req,res) => {
    try {
        let time = new Date();
        req.body.booksId = parseInt(req.body.booksId);
        const sql = 'insert into record set accounts=?,booksId=?,startTime=?';
        db.query(sql,[req.user.accounts,req.params.booksId,time],(err,result) => {
            if (err) return  returnData(res,500,err);
            if (result.affectedRows !== 1) return returnData(res,400,'借阅失败');
            return returnData(res,200,'借阅成功');
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    获取所有的借阅记录---视图
 */
exports.getAllBooksRecord = (req,res) => {
    try {
        let m = (parseInt(req.body.pageNumber) - 1) * parseInt(req.body.pageSize);
        let n =  parseInt(req.body.pageNumber) * parseInt(req.body.pageSize);
        const sql = 'select * from recorduser limit ?,?';
        db.query(sql,[m,n],(err,result) => {
            if (err) return  returnData(res,500,err);
            return returnData(res,200,'获取成功',{data: result,total: req.body.total});
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    获取自己的借阅记录
 */
exports.getMyBooksRecord = (req,res) => {
    try {
        const sql = 'select * from recorduser where accounts=?';
        db.query(sql,[req.user.accounts],(err,result) => {
            if (err) return  returnData(res,500,err);
            return returnData(res,200,'获取成功',{data: result});
        });
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

/*
    还书
 */
exports.UserBooksRecord = (req,res) => {
    try {
        const sql2 = 'update books set number=? where id=?';
        db.query(sql2,[req.body.number,req.body.booksId],(err,result) => {
            if (err) return  returnData(res,500,err);
            if (result.affectedRows !== 1) return returnData(res,500,'异常');
            const sql = 'update record set isSus=1 where id=?';
            db.query(sql,req.params.recordId,(err,result) => {
                if (err) return  returnData(res,500,err);
                if (result.affectedRows !== 1) return returnData(res,500,'异常');
                return returnData(res,200,'还书成功');
            })
        })
    } catch (e) {
        return returnData(res,500,'异常');
    }
}

